package com.grm.sys.service.impl;

import com.grm.common.exception.BusinessException;
import com.grm.sys.mapper.SysPostMapper;
import com.grm.sys.model.SysPost;
import com.grm.sys.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysPostServiceImpl implements SysPostService {
    @Autowired
    private SysPostMapper sysPostMapper;
    @Override
    public List<SysPost> queryAll() {
        return  sysPostMapper.queryAll();
    }

    @Override
    public List<SysPost> queryListSelective(SysPost queryParam) {
        return sysPostMapper.queryList(queryParam);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public void add(SysPost sysPost) {
        sysPostMapper.insert(sysPost);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public void update(SysPost sysPost) {
        SysPost dbSysPost = sysPostMapper.queryById(sysPost.getId());
        if (dbSysPost == null) {
            throw new BusinessException("岗位不存在！");
        }
        // 防止绕过前台篡改岗位编码
        sysPost.setCode(dbSysPost.getCode());
        sysPostMapper.update(sysPost);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public void delete(List<Long> ids) {
        sysPostMapper.deleteByIds(ids);
    }
}
